package org.example.SolutionNew003;

public class DoubleTree007 {
    public static void main(String[] args) {
        DoubleTree007 dt = new DoubleTree007();
        int[] nums=new int[]{1,2,3,4,5,6,7,8,9};
        TreeNode res=dt.sortedArrayToBST(nums);
        System.out.println(res.val);
        System.out.println(res.left.val);
        System.out.println(res.right.val);
        System.out.println(res.left.left.val);
        System.out.println(res.left.right.val);
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        //二叉平衡树->二分
        int start = 0,end=nums.length-1;
        return DoubleBestTree(nums,start,end);
    }

    public TreeNode DoubleBestTree(int[] nums, int start, int end) {
        if (start > end) return null;
        int mid=start+(end-start)/2;
        TreeNode node=new TreeNode(nums[mid]);
        node.left=DoubleBestTree(nums,start,mid-1);
        node.right=DoubleBestTree(nums,mid+1,end);
        return node;
    }
}
